package com.bangcommunity.bbframe.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author tanghc
 */
public class RegionUtils {
	private static Logger logger = LoggerFactory.getLogger(RegionUtils.class);

	public static boolean isRegionCode(int regionCode) {
		return regionCode >= 100000 && regionCode < 1000000;
	}

	public static boolean isProvince(int regionCode) {
		return isRegionCode(regionCode) && (regionCode % 10000) == 0;
	}

	public static boolean isCity(int regionCode) {
		return isRegionCode(regionCode) && !isProvince(regionCode) && (regionCode % 100) == 0;
	}
	public static boolean isDistrict(int regionCode) {
		return isRegionCode(regionCode) && !isProvince(regionCode) && !isCity(regionCode);
	}
	public static Integer getCity(int regionCode) {
		checkRegionCode(regionCode);
		if(isProvince(regionCode)){
			return null;
		}
		return regionCode/100 * 100;
	}
	public static Integer getProvice(int regionCode) {
		checkRegionCode(regionCode);
		return regionCode/10000 * 10000;
	}
	private static void checkRegionCode(int regionCode){
		if(!isRegionCode(regionCode)){
			throw new RuntimeException("invalid region code "+regionCode);
		}
	}
}
